package com.j180.controller.store;

import com.j180.bean.*;
import com.j180.service.store.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private IStoreService iStoreService;

    /**
     *    平台管理员查询所有门店信息
     */
    @GetMapping("/showAllStore")
    public ResultBean<List<Store>> showAllStoreInfo(){
        ResultBean<List<Store>> AllStoreInfo = new ResultBean<>();

        List<Store>  stores = iStoreService.showAllStoreInfo();

        if(stores.size()==0){
            AllStoreInfo.setCode(0);
            AllStoreInfo.setMsg("没有找到符合条件的数据");
            AllStoreInfo.setT(null);
        }else{
            AllStoreInfo.setCode(1);
            AllStoreInfo.setMsg("找到"+stores.size()+"条,符合条件的数据");
            AllStoreInfo.setT(stores);
        }

        return AllStoreInfo;
    }

    /**
     * 平台管理员根据门店ID查询门店信息
     * @param id 门店id
     *
     */
    @GetMapping("/showDetailStoreInfo/{storeid}")
    public ResultBean<Store>  showStoreDetailInfoById(@PathVariable("storeid")int id){
        ResultBean<Store> storeDetailInfoForId = new ResultBean<>();

        Store store = iStoreService.showStoreDetailInfoById(id);

        if(store==null){
            storeDetailInfoForId.setCode(0);
            storeDetailInfoForId.setMsg("没有查到符合条件的数据");
            storeDetailInfoForId.setT(null);
        }else{
            storeDetailInfoForId.setCode(1);
            storeDetailInfoForId.setMsg("找到符合条件的数据信息");
            storeDetailInfoForId.setT(store);
        }


        return storeDetailInfoForId;
    }

    /**
     * 门店管理员根据门店id查询自己门店详细信息
     * @param id  门店iD
     * @return  查询结果
     */
    @GetMapping("/showAllStoreForConnect/{storeid}")
    public ResultBean<List<Store>> getAllStoreInfoForConnect(@PathVariable("storeid")int id){
        ResultBean<List<Store>> storeInfoForConnet = new ResultBean<>();

        List<Store> stores = iStoreService.getAllStoreInfoForConnect(id);

        if(stores.size()==0){
            storeInfoForConnet.setCode(0);
            storeInfoForConnet.setMsg("没有查到符合条件的数据");
            storeInfoForConnet.setT(null);
        }else{
            storeInfoForConnet.setCode(1);
            storeInfoForConnet.setMsg("找到"+stores.size()+"条,符合条件的数据");
            storeInfoForConnet.setT(stores);
        }

        return  storeInfoForConnet;
    }

    /**
     * 新增门店
     * @param store  传入门店实体
     * @return 成功 'success' 失败 'failure'
     */
    @PostMapping("/add")
    public ResultBean<String> addStore(@RequestBody(required = false) Store store, HttpServletRequest request){
        User u = new User();
        u.setId(2);
        store.setUser(u);
        ResultBean<String> addStore = new ResultBean<>();

        int num = iStoreService.addStore(store);

                if(num==0){
                    addStore.setCode(0);
                    addStore.setMsg("添加失败");
                    addStore.setT("failure");
                }else{
                    addStore.setCode(1);
                    addStore.setMsg("添加成功");
                    addStore.setT("success");
                }
        return addStore;
    }

    /**
     * 根据门店ID删除门店
     * @param id 要删除门店的ID
     * @return 成功 'success' 失败 'failure'
     */
    @DeleteMapping("/delete/{id}")
    public ResultBean<String> delStore(@PathVariable("id") int id){
        ResultBean<String> delStore = new ResultBean<>();
        int num = iStoreService.delStore(id);

        if(num==0){
            delStore.setCode(0);
            delStore.setMsg("删除失败");
            delStore.setT("failure");
        }else{
          delStore.setCode(1);
          delStore.setMsg("删除成功");
          delStore.setT("success");
        }
        return delStore;
    }


    /**
     * 更新修改门店信息
     * @param store  传入门店实体
     * @return 成功 'success' 失败 'failure'
     */
    @PutMapping("/edit")
    public ResultBean<String> editStore(@RequestBody(required = false) Store store){
        ResultBean<String> editStore = new ResultBean<>();
        System.out.println(store);
        int num = iStoreService.editStore(store);

        if(num==0){
            editStore.setCode(0);
            editStore.setMsg("修改失败");
            editStore.setT("failure");
        }else{
            editStore.setCode(1);
            editStore.setMsg("修改成功");
            editStore.setT("success");
        }

        return editStore;
    }

    /**
     * 分页
     * @param curPage  分页页数
     * @param pageSize  每页显示条数
     */
    @GetMapping("/paging/{curPage}/{pageSize}")
    public ResultBean <PageBean> pageInfo(@PathVariable("curPage") String curPage, @PathVariable("pageSize") String pageSize){
        ResultBean <PageBean> Forpaging = new ResultBean<>();

        PageBean pageBean = iStoreService.pageInfo(Integer.parseInt(curPage), Integer.parseInt(pageSize));

        if(pageBean.getDatas().size() == 0){
            Forpaging.setCode(0);
            Forpaging.setMsg("没有符合条件的数据");
            Forpaging.setT(null);
        }else{
            Forpaging.setCode(1);
            Forpaging.setMsg("找到"+pageBean.getTolNum()+"条,符合条件的数据");
            Forpaging.setT(pageBean);
        }

        return Forpaging;
    }

    /**
     * 根据门店名称查询门店详细信息
     * @param name 门店名称
     * @return 查询结果
     */
    @GetMapping("/showStoreInfoByStoreName/{name}")
    public ResultBean <List<Store>> showStoreDetailInfoByStoreName(@PathVariable("name") String name){
        ResultBean <List<Store>> storeInfoForStoreName= new ResultBean<>();

        List<Store> stores = iStoreService.showStoreDetailInfoByStoreName(name);

        if(stores.size()==0){
            storeInfoForStoreName.setCode(0);
            storeInfoForStoreName.setMsg("没有查到符合条件的数据");
            storeInfoForStoreName.setT(null);
        }else{
            storeInfoForStoreName.setCode(1);
            storeInfoForStoreName.setMsg("找到"+stores.size()+"条,符合条件的数据");
            storeInfoForStoreName.setT(stores);
        }

        return storeInfoForStoreName;
    }

    /**
     * 门店管理员根据门店ID和开始时间到结束时间查看销售数量
     * @param id 门店ID
     * @return 查询结果集
     */
    @GetMapping("/showStoreSalesVolumeForDate/{id}")
    public ResultBean<List<StoreCount>> showStoreSalesVolumeForDate(@PathVariable("id") int id) {
        ResultBean<List<StoreCount>> StoreSalesVolumeForDate = new ResultBean<>();

        List<StoreCount> storeCounts = iStoreService.showStoreSalesVolumeForDate(id);

        if(storeCounts.size()==0){
            StoreSalesVolumeForDate.setCode(0);
            StoreSalesVolumeForDate.setMsg("没有查询到符合条件的信息");
            StoreSalesVolumeForDate.setT(null);
        }else{
            StoreSalesVolumeForDate.setCode(1);
            StoreSalesVolumeForDate.setMsg("查询到"+storeCounts.size()+"条,符合条件的数据");
            StoreSalesVolumeForDate.setT(storeCounts);
        }
        return StoreSalesVolumeForDate;
    }
}
